﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TodayApp.Services;

namespace TodayApp.ViewModels;

[ObservableObject]
public partial class MainPageViewModel {
    private readonly ITodayPoetryService _todayPoetryService;

    private readonly ITodayImageService _todayImageService;

    public MainPageViewModel(ITodayPoetryService todayPoetryService,
        ITodayImageService todayImageService) {
        _todayPoetryService = todayPoetryService ??
            throw new ArgumentNullException(nameof(todayPoetryService));
        _todayImageService = todayImageService ??
            throw new ArgumentNullException(nameof(todayImageService));
    }

    [ObservableProperty] private string _snippet;

    [ObservableProperty] private ImageSource _imageSource;

    [RelayCommand]
    private async Task UpdateAsync() {
        var todayImage = await _todayImageService.GetTodayImageAsync();
        ImageSource =
            Microsoft.Maui.Controls.ImageSource.FromStream(() =>
                new MemoryStream(todayImage.ImageBytes));

        var todayPoetry = await _todayPoetryService.GetTodayPoetryAsync();
        Snippet = todayPoetry.Snippet;
    }
}